package com.elite.groovy.kh.al;

import com.alibaba.nls.client.AccessToken;
import com.elite.groovy.kh.KHUtil;
import com.elite.groovy.util.Util;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author siwei
 * @date 2019-10-30
 */
public class CreateToken {
    private static final Log log = LogFactory.getLog(CreateToken.class);

    public static void main(String[] args) {

        String res_Text = "账号管理";
        String accessKeyId = "LTAI5tKC9nScDe6B77ocmqqN";
        String accessKeySecret = "JJDA4twalK7zviUlvzA5UxEzsSm8Ef";
        System.out.println("accessKeyId=" + accessKeyId + "; accessKeySecret=" + accessKeySecret);
        AccessToken accessToken = new AccessToken(accessKeyId, accessKeySecret);
        try {
            accessToken.apply();
            log.debug("Token: " + accessToken.getToken() + ", expire time: " + accessToken.getExpireTime());

            String audio_address = "https://ccctest.davco.cn/ngs/fs/get?file=/temp//Upload_Files/20240805171254231.mp3";
            String call_URL = "https://nls-gateway-cn-shanghai.aliyuncs.com/stream/v1/FlashRecognizer?appkey=CnVAsCQQaPZkUv60&token=" + accessToken.getToken() + "&format=mp3&sample_rate=16000&audio_address=" + audio_address;
            log.debug("请求URL：" + call_URL);

            // 处理头文件
            Map<String, String> headerMap = new HashMap<String, String>();
            headerMap.put("Content-type", "application/text");

            String postResult = Util.doHttpPostUseHead(call_URL, "", "UTF-8", 120000, 0, headerMap);
            log.debug("返回请求体：" + postResult);

            JSONObject res_Body = JSONObject.fromObject(postResult);

            String status = KHUtil.GetSFJV(res_Body,"status");
            String message = KHUtil.GetSFJV(res_Body,"message");
            if(status.equals("20000000")){
                res_Text= res_Body.getJSONObject("flash_result").getJSONArray("sentences").getJSONObject(0).getString("text");
            }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}